package classDemo;


import org.apache.log4j.Logger;
//import sun.rmi.rmic.iiop.ClassPathLoader;

/**
 * @author Ajie
 * @date 2021/11/9
 * @function
 * Class.forName(), newInstance(), New 三者区别
 *
 * Class.forName(),
 * 返回 Class<?> aClass 类的类对象。
 * 不会初始化，构造方法，会初始化静态变量，静态代码块。
 * Class<?> aClass = Class.forName("classDemo.TestClassNewIns");
 *
 * Object o = aClass.newInstance();
 * 返回class 的实例化，对象。
 * 只是利用了反射。只能 使用空参数，构造。
 *
 * new()
 * 正常新建对象。
 *
 */
public class Class_NewInstance_ForName_New {
    private static final Logger logger = Logger.getLogger(Class_NewInstance_ForName_New.class);
    public Class_NewInstance_ForName_New() {
        System.out.println("Class_NewInstance_ForName_New constract");
    }

    public static void main(String[] args) {
        try {
            Class<?> aClass = Class.forName("classDemo.TestClassNewIns");

//            Class<?> aClass = ClassPathLoader.getSystemClassLoader().loadClass("classDemo.TestClassNewIns");
//            Class<?> aClass = ClassPathLoader.getSystemClassLoader().loadClass("classDemo.TestClassNewIns",false);
            logger.info("main: startSleep");
            Thread.sleep(1000);
//            Object o = aClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

class TestClassNewIns{
    static TestClassNewIns_TWO ins_two = new TestClassNewIns_TWO();
    static {
        System.out.println("static code {}");
    }
    public TestClassNewIns() {
        System.out.println("constract");
        //拿到调用栈
       try {
           throw new Exception("拿到调用栈 get constract track");
       }catch (Exception e){
           e.printStackTrace();
       }
    }
}
class TestClassNewIns_TWO{
    public TestClassNewIns_TWO() {
        System.out.println("TestClassNewIns_TWO , constract");
    }
}
